<?php
if($_GET["query"])
{
        $sql=$_GET["query"];

        $conn = mysql_connect("localhost","cs143","");
        mysql_select_db("CS143",$conn);

        $result = mysql_query($sql, $conn);

        mysql_close($conn);
}
?>
<html>
<head>
<title>LAB 1B</title>
</head>
<body>
Type an SQL query in the following box:

<form action="./query.php" method="GET">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="submit" />
</form>
<?php
	if($_GET['query'])
	{
		echo "The query you type is: ".$_GET['query']."<br>";
		
	}

//count fields number
//
	
        if($result)
        {
                echo "<table border=1>";
		$temp = mysql_num_fields($result);
		echo "<tr>";
		echo $temp;
		$j=0;
			for($j;$j<$temp; $j++)
			{	
				echo "<td>";
				echo "<b>".mysql_field_name($result,$j)."</b>";
				echo "</td>";
			}
		echo "</tr>";
                while($line = mysql_fetch_row($result))
                {	
		
			
			
			echo "<tr>";
			
			
			
			$i=0;
			for($i ; $i < count($line);$i++)
			{
				echo "<td>";
				echo $line[$i];
				echo "</td>"; 

			}
                        		
                        echo "</tr>";
			$j++;
                }
                echo "</table>";
                                   
        }


// practice for queries: select first, last from Actor where id in(select aid from MovieActor where mid in (select id from Movie where title='Die Another Day'))
//select * from Actor where id in (select aid from MovieActor group by aid having count(mid)>=2)
?>

</body>
</html>

